package wowmobileclient;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.StreamConnection;

public class Communication {

    private DataInputStream in;
    private DataOutputStream out;

    public Communication(StreamConnection channel) {
        try {
            InputStream is = channel.openInputStream();
            OutputStream os = channel.openOutputStream();
            out = new DataOutputStream(os);
            in = new DataInputStream(is);
        } catch (Exception e) {
            System.err.println("Error in Communication Constructor" + e);
            return;
        }
    }

    public void sendAction(Action a) throws IOException {
        String s = a.toString();
      //  System.out.println(s);
        out.writeUTF(s);
    }

    public void sendString(String s)
    {
        if(!s.equals("n"))
            System.out.println(s);
        try {
            out.writeUTF(s);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public Action getAction() throws IOException, ClassNotFoundException {
        Action a;
        String s;
        s = in.readUTF();
        a = Action.fromString(s);
        return a;
    }

    public String getPlayers() throws IOException {
        String s = in.readUTF();
        while(s.equals("F"))
            s=in.readUTF();
        return s;
    }

    public String getArea() throws IOException {
        String s = in.readUTF();
        return s;
    }

    public String getAnswer() throws IOException {
        
        String s = in.readUTF();
        System.out.println("Test: "+s);
        return s;
    }

    public int[] getStartFight() throws IOException
    {
        String s=in.readUTF();
        int[] startFight=splitStartFight(s);
        return startFight;
    }

    public int[] splitStartFight(String s)
    {
        int[] startFight=new int[2];
        String[] tmp = SplitString.split(s, " ");
        for (int i = 0; i < tmp.length; i++) {
            startFight[i] = Integer.parseInt(tmp[i]);
        }
        return startFight;
    }
    public boolean getIsLogin() throws IOException{
        String s = in.readUTF();
        boolean isLogin;
        if (s.equals("true")){
            isLogin = true;
        } else {
            isLogin = false;
        }
        return isLogin;
    }
}
